package conf2.ui;

import conf2.model.EActor;
import conf2.text.Actors;
import conf2.text.Misc;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import static conf2.model.EActor.*;

public class DraftGameDialog extends JDialog {

    private EActor.Side side;
    private EActor[] classicChars,  variantChars;
    private Set<EActor> selected = new HashSet<EActor>();
    private Map<EActor, ImageIcon> icons = new EnumMap<EActor, ImageIcon>(EActor.class);
    private JButton okButton;

    public List<EActor> getSelected() {
        return new ArrayList<EActor>(selected);
    }

    public static List<EActor> show(Frame parent, EActor.Side side) {
        DraftGameDialog dlg = new DraftGameDialog(parent, side);
        dlg.setVisible(true);
        return dlg.getSelected();
    }

    private DraftGameDialog(Frame parent, EActor.Side side) {
        super(parent, true);
        setUndecorated(true);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        this.side = side;

        Color fg, bg, val;
        if (side == EActor.Side.LIGHT) {
            fg = ColorSet.LIGHTCARD_TEXT;
            bg = ColorSet.SHIRE_BG;
            val = ColorSet.DARKCARD_BG;
            classicChars = new EActor[]{
                        C_FRODO, C_SAM, C_GANDALF, C_ARAGORN, C_MERRY, C_PIPPIN, C_LEGOLAS, C_GIMLI, C_BOROMIR
                    };
            variantChars = new EActor[]{
                        V_FRODO, V_SAM, V_GANDALF, V_ARAGORN, V_FARAMIR, V_SMEAGOL, V_ELROND, V_THEODEN, V_TREEBEARD
                    };
        } else {
            fg = ColorSet.DARKCARD_TEXT;
            bg = ColorSet.MORDOR_BG;
            val = ColorSet.LIGHTCARD_BG;
            classicChars = new EActor[]{
                        C_SARUMAN, C_WITCHKING, C_ORCS, C_F_NAZGUL, C_CAVETROLL, C_BALROG, C_WARG, C_SHELOB, C_B_RIDERS
                    };
            variantChars = new EActor[]{
                        V_SARUMAN, V_WITCHKING, V_ORCS, V_F_NAZGUL, V_WATCHER, V_URUKHAI, V_GOLLUM, V_WORMTONGUE, V_MOUTHOFSAURON
                    };
        }
        getContentPane().setBackground(bg);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout(10, 10));
        mainPanel.setBackground(bg);
        mainPanel.setBorder(BorderFactory.createRaisedBevelBorder());

        JPanel p = new JPanel(new GridLayout(1, 3, 20, 0));
        p.setBackground(bg);

        JLabel caption = new JLabel(Misc.DRAFT_DLG_CAPTION, SwingConstants.CENTER);
        caption.setFont(FontSet.getDraftCaption());
        caption.setForeground(fg);
        mainPanel.add(caption, BorderLayout.NORTH);

        final JLabel nameLabel = new JLabel();
        nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        nameLabel.setForeground(fg);
        nameLabel.setFont(FontSet.getDraftCharacterName());

        final JLabel attackLabel = new JLabel();
        attackLabel.setHorizontalAlignment(SwingConstants.CENTER);
        attackLabel.setForeground(val);
        attackLabel.setFont(FontSet.getActorviewerAttackValue());

        final JLabel portraitLabel = new JLabel();

        final JLabel descLabel = new JLabel();
        descLabel.setForeground(fg);
        descLabel.setPreferredSize(new Dimension(200, 200));
        descLabel.setFont(FontSet.getActorviewerText());
        descLabel.setVerticalAlignment(SwingConstants.TOP);

        JPanel[] col = new JPanel[3];
        for (int i = 0; i < 2; i++) {
            col[i] = new JPanel(new GridLayout(5, 2, 5, 5));
            col[i].setBackground(bg);
            p.add(col[i]);
        }

        col[2] = new JPanel();
        col[2].setLayout(new FlowLayout(FlowLayout.CENTER));
        col[2].setBackground(bg);

        col[2].add(nameLabel);

        JPanel subPanel = new JPanel(new GridLayout(1, 2));
        subPanel.setBackground(bg);
        subPanel.add(portraitLabel);
        subPanel.add(attackLabel);
        col[2].add(subPanel);

        col[2].add(descLabel);

        p.add(col[2]);

        for (int i = 0; i < 9; i++) {
            ImageIcon classicIcon = new ImageIcon(Portraits.get(classicChars[i]));
            icons.put(classicChars[i], classicIcon);
            final JLabel classic = new JLabel(classicIcon);
            int colIx = i / 5;
            col[colIx].add(classic);
            selected.add(classicChars[i]);

            final int curIndex = i;

            ImageIcon variantIcon = new ImageIcon(Portraits.get(variantChars[i]));
            icons.put(variantChars[i], variantIcon);
            final JLabel variant = new JLabel(variantIcon);
            variant.setEnabled(false);
            col[colIx].add(variant);

            classic.addMouseListener(new MouseAdapter() {

                @Override
                public void mousePressed(MouseEvent e) {
                    classic.setEnabled(true);
                    variant.setEnabled(false);
                    selected.remove(variantChars[curIndex]);
                    selected.add(classicChars[curIndex]);
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    EActor a = classicChars[curIndex];
                    nameLabel.setText(Actors.getName(a));
                    attackLabel.setText(String.valueOf(a.getDefaultAttackValue()));
                    portraitLabel.setIcon(icons.get(a));
                    descLabel.setText("<html>" +Actors.getDescription(a) + "</html>");
                }
            });


            variant.addMouseListener(new MouseAdapter() {

                @Override
                public void mousePressed(MouseEvent e) {
                    variant.setEnabled(true);
                    classic.setEnabled(false);
                    selected.remove(classicChars[curIndex]);
                    selected.add(variantChars[curIndex]);
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    EActor a = variantChars[curIndex];
                    nameLabel.setText(Actors.getName(a));
                    attackLabel.setText(String.valueOf(a.getDefaultAttackValue()));
                    portraitLabel.setIcon(icons.get(a));
                    descLabel.setText("<html>" + Actors.getDescription(a) + "</html>");
                }
            });
        }

        mainPanel.add(p, BorderLayout.CENTER);

        okButton = new JButton("<html>" + Misc.DRAFT_CONTINUE_BUTTON_LABEL + "</html>");
//        okButton.setToolTipText("<html>Confirm your selection of<br>characters, and start the game.</html>");

        final JDialog dlg = this;
        okButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                List<EActor> selected = getSelected();
                Collections.sort(selected, new Comparator<EActor>() {

                    public int compare(EActor o1, EActor o2) {
                        return o1.name().compareTo(o2.name());
                    }
                });
                StringBuilder sb = new StringBuilder("<ul>");
                for (EActor ea : selected) {
                    String name = Actors.getName(ea);
                    String varStr = "";
                    if (ea.isVariant()) {
                        varStr = " " + Misc.DRAFT_CONFIRM_MESSAGE_VARIANT;
                    }
                    sb.append(String.format("<li>%s%s</li>", name, varStr));
                }
                sb.append("</ul>");
                String msg = "<html>" + String.format(Misc.DRAFT_CONFIRM_MESSAGE, sb.toString()) + "</html>";
                int reply = MessageBox.yesNo(dlg, msg, Misc.DRAFT_CONFIRM_MESSAGE_CAPTION, null, true);
                if (reply == JOptionPane.YES_OPTION) {
                    dlg.dispose();
                }
            }
        });

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.setBackground(new Color(0, 0, 0, 127));
        buttonPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        buttonPanel.add(okButton);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(mainPanel);
        pack();
        setLocationRelativeTo(parent);
    }
}